<template>
  <div class="app-container">
    <div class="inside-container">
      <el-form :rules="rules" :label-position="labelPosition" label-width="80px" :model="accountInfoForm" ref="accountInfoForm">
        <el-form-item label="更换头像">
          <el-upload
            class="avatar-uploader"
            action="http://114.132.200.186:8080/uploadAvatar"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
          <el-button type="primary" @click="handleUpdateAvatar">提交</el-button>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="accountInfoForm.empId" disabled />
        </el-form-item>
        <el-form-item label="原密码">
          <el-input v-model="accountInfoForm.oldPwd" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input v-model="accountInfoForm.newPwd" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPwd">
          <el-input v-model="accountInfoForm.confirmPwd" show-password />
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="updatePassword('accountInfoForm')">更 改 密 码</el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { updateAvatar } from '@/api/news'

export default {
  name: 'AccountInfo',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.accountInfoForm.confirmPwd !== '') {
          this.$refs.accountInfoForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.accountInfoForm.newPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      labelPosition: 'left',
      imageUrl: '',
      imgPath: '',
      accountInfoForm: {
        empId: '',
        oldPwd: '',
        newPwd: '',
        confirmPwd: ''
      },
      rules: {
        newPwd: [
          { validator: validatePass, trigger: 'blur' }
        ],
        confirmPwd: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'empId'
    ])
  },
  created() {
    this.accountInfoForm['empId'] = this.$route.params.id
  },
  methods: {
    updatePassword(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('waiting to complete')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleUpdateAvatar() {
      const params = {
        empId: this.accountInfoForm.empId,
        path: this.imgPath
      }
      updateAvatar(params).then(response => {
        this.$alert('更新成功', '提示', {
          confirmButtonText: '确定'
        })
      })
    },
    handleAvatarSuccess(res, file) {
      console.log(res)
      this.imageUrl = URL.createObjectURL(file.raw)
      console.log(this.imageUrl)
      this.imgPath = res.content
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style scoped>
.app-container {
  width: 1400px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.inside-container {
  width: 500px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
